package com.feicheng.lanniao.service.impl;

import com.feicheng.lanniao.entity.Car;
import com.feicheng.lanniao.entity.CarSellClientVo;
import com.feicheng.lanniao.entity.CarSellInfo;
import com.feicheng.lanniao.exception.SystemException;
import com.feicheng.lanniao.mapper.CarSellInfoMapper;
import com.feicheng.lanniao.result.ResultInfo;
import com.feicheng.lanniao.service.ICarSellInfoService;
import com.feicheng.lanniao.service.ICarService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author feichengTech
 * @since 2021-09-02
 */
@Service
public class CarSellInfoServiceImpl extends ServiceImpl<CarSellInfoMapper, CarSellInfo> implements ICarSellInfoService {

	@Autowired
	ICarService iCarService;

	/**
	 * @see.ICarSellInfoService.deleteCarSellInfoById
	 */
	@Override
	public ResultInfo deleteCarSellInfoById(Integer carSellInfoId) {
		//如果carSellInfoId为空或0，抛出异常
		if(null==carSellInfoId || 0 ==carSellInfoId) {
			throw new SystemException("carSellInfoId为空");
		}
		// 构建查询条件
		QueryWrapper<CarSellInfo> carQueryWapper = new QueryWrapper<CarSellInfo>();
		// 把主键加入查询条件
		carQueryWapper.eq("car_sell_info_id", carSellInfoId);
		 // 根据主键查询出一条数据，用一个对象接收
		CarSellInfo carSellInfo = this.getOne(carQueryWapper);
		// 设置删除位值
		carSellInfo.setDelFlag(false);
		// 修改车辆
		Boolean b = this.updateById(carSellInfo);
		// 返回修改结果
		if (!b) {
			throw new SystemException("删除失败");
		}
		return ResultInfo.success();
	}
	/**
	 * @see.ICarSellInfoService.updateCarSellInfo
	 */
	@Override
	public ResultInfo updateCarSellInfo(CarSellClientVo carSellClientVo) {
		Car car = new Car();
		car.setCarId(carSellClientVo.getCarId());
		car.setCarCondition(carSellClientVo.getCarCondition());
		car.setMileage(carSellClientVo.getMileage());
		car.setOwnerShip(carSellClientVo.getOwnerShip());
		car.setBrandId(carSellClientVo.getBrandId());
		car.setType(carSellClientVo.getType());
		car.setImgUrl(carSellClientVo.getImgUrl());
		car.setSellPrice(carSellClientVo.getSellPrice());
		car.setImgUrl(carSellClientVo.getImgUrl());
		// 判断车辆ID是否存在,若不存在抛出异常
		if (null == iCarService.getById(car.getCarId())) {
			throw new SystemException("车辆ID缺失,无法更新");
		}
		CarSellInfo carSellInfo = new CarSellInfo();
		carSellInfo.setCarSellInfoId(carSellClientVo.getCarSellInfoId());
		carSellInfo.setCarId(carSellClientVo.getCarId());
		carSellInfo.setSellPrice(carSellClientVo.getSellPrice());
		carSellInfo.setTel(carSellClientVo.getTel());
		carSellInfo.setWxNumber(carSellClientVo.getWxNumber());
		// 判断卖车ID是否存在,若不存在抛出异常
		if (null == this.getById(carSellInfo.getCarSellInfoId())) {
			throw new SystemException("卖车ID缺失,无法更新");
		}
		// 修改车辆
		iCarService.updateById(car);
		//修改卖车信息
		this.updateById(carSellInfo);
		return ResultInfo.success();
	}

}
